package com.wonders.module.auth.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wonders.common.constant.CommonConstant;
import com.wonders.common.constant.TipConstant;
import com.wonders.framework.entity.vo.Page;
import com.wonders.framework.util.StringUtil;
import com.wonders.framework.util.ValidateUtil;
import com.wonders.framework.util.web.RequestUtil;
import com.wonders.framework.web.BaseController;
import com.wonders.module.auth.entity.vo.RoleVo;
import com.wonders.module.auth.service.RoleService;

/**
 * 角色控制层
 * 
 * @author WuBin
 * 
 */
@Controller
@RequestMapping("/auth/role")
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService;

	/**
	 * 导航到权限列表页面
	 * 
	 * @return
	 */
	@RequestMapping("toQuery")
	public Object toQuery() {
		String action = (String) RequestUtil.getRequestParam("action");
		RequestUtil.setRequestAttr("action", action);
		return "auth/roleList";
	}

	/**
	 * 权限信息列表查询
	 * 
	 * @return
	 */
	@RequestMapping("query")
	@ResponseBody
	public Object query() {
		RoleVo roleVo = RequestUtil.requestParamToBean(RoleVo.class);
		roleVo.setEnable(CommonConstant.Status.ENABLED);
		Page page = roleService.query(roleVo, super.getDataGridPage());
		return super.dataGridResult(page);
	}

	/**
	 * 导航到菜单信息编辑页面
	 * 
	 * @return
	 */
	@RequestMapping("toForm")
	public Object toForm() {
		return "auth/roleForm";
	}

	/**
	 * 保存菜单信息
	 * 
	 * @return
	 */
	@RequestMapping("save")
	@ResponseBody
	public Object save() {
		RoleVo roleVo = RequestUtil.requestParamToBean(RoleVo.class);
		String action = (String) RequestUtil.getRequestParam("action");
		if (StringUtils.isEmpty(action)
				|| !ValidateUtil.nullCheck(roleVo, "code", "name")) {
			return super.ajaxError(TipConstant.PARAM.MISSING);
		}
		if (CommonConstant.Action.EDIT.equals(action)) {
			if (!ValidateUtil.nullCheck(roleVo, "id")) {
				return super.ajaxError(TipConstant.PARAM.MISSING);
			}
			roleService.update(roleVo);
		} else if (CommonConstant.Action.ADD.equals(action)) {
			roleService.save(roleVo);
		} else {
			return super.ajaxError(TipConstant.PARAM.INVALID);
		}
		return super.ajaxSuccess();
	}

	/**
	 * 加载菜单信息
	 * 
	 * @return
	 */
	@RequestMapping("load")
	@ResponseBody
	public Object load() {
		RoleVo roleVo = RequestUtil.requestParamToBean(RoleVo.class);
		if (!ValidateUtil.nullCheck(roleVo, "id")) {
			return super.ajaxError(TipConstant.PARAM.MISSING);
		}
		roleVo = roleService.load(roleVo.getId());
		return super.ajaxSuccess(roleVo);
	}

	/**
	 * 删除菜单信息
	 * 
	 * @return
	 */
	@RequestMapping("remove")
	@ResponseBody
	public Object remove() {
		String keys = (String) RequestUtil.getRequestParam("keys");
		if (StringUtils.isEmpty(keys)) {
			return super.ajaxError(TipConstant.PARAM.MISSING);
		}
		roleService.remove(StringUtil.stringToList(keys, Long.class));
		return super.ajaxSuccess();
	}

}
